package com.company.recruit.clean.demo2;

import com.company.recruit.controller.RecruitReduce;
import com.company.recruit.entity.RecruitDaoEntity;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.springframework.util.StringUtils;

import java.io.IOException;

//import com.company.recruit.test.Test;

/**
 * @author wanghengfeng
 * @date 2021年/12月/02日  12:20:56
 */
public class MyPositionMapper extends Mapper<LongWritable, Text, Text, Text> {

    private static final String NUM = "0";
    private static final int LINE_LENGTH = 10;





    /**
     *  一定要注意cvs乱码  -- 把文件用记事本打开 -- 另存为 utf-8
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        if (!NUM.equals(key.toString())) {
            String[] line = value.toString().split(",");


            RecruitDaoEntity recruitDaoEntity = new RecruitDaoEntity();

//            Arrays.stream(line).map((l)->{
//                System.out.println(l.toString());
//                return 0;
//            });
//            if (line.length > LINE_LENGTH) {
            // 计算工资
            String wage = line[2];
            float[] wages = processSalary51(wage);
            float minWage = wages[0];
            float maxWage = wages[1];

            String staff = line[6];
            Integer[] staffs = processStaff(line[6]);
            Integer minStaff = staffs[0];
            Integer maxStaff = staffs[1];

            // 工资
            recruitDaoEntity.setWages(line[2]);
            // 经验
            recruitDaoEntity.setExperience(line[3]);
            // 学历
            recruitDaoEntity.setEducation(line[4]);
            // 城市
            recruitDaoEntity.setCity(line[1]);
            // 职位
            recruitDaoEntity.setPost(line[0]);
            // 公司
            recruitDaoEntity.setCompany(line[5]);
            // 员工
            recruitDaoEntity.setScale(line[6]);
            // 性质
            recruitDaoEntity.setNature(line[7]);
            recruitDaoEntity.setMinwage(minWage);
            recruitDaoEntity.setMaxwage(maxWage);
            recruitDaoEntity.setMinstaff(minStaff);
            recruitDaoEntity.setMaxstaff(maxStaff);
            recruitDaoEntity.setDetail(line[9]);

            if (!StringUtils.isEmpty(recruitDaoEntity)) {
                // System.out.println(recruitEntity.toString());
//                    recruitService.save(recruitEntity);
                //  recruitDao.insert(recruitEntity);
                // 传递实体
//                    recruitReduce.deliveryEntity(recruitEntity);
//                 test.addUser(recruitDaoEntity);
            }


            context.write(new Text("工资"), new Text(minWage + "\t" + maxWage + "\t" + line[0] + "\t" + line[1] + "\t"
                    + line[3] + "\t" + line[4] + "\t" + line[5] + "\t" + line[6] + "\t" + line[7] + "\t" + line[8] + "\t" + line[9]));
        }

//        }
    }

    /**
     * 处理员工人数
     *
     * @param staff 员工字符串
     * @return integer[]
     */
    private Integer[] processStaff(String staff) {
        Integer minStaff = 0;
        Integer maxStaff = 0;

        try {
            if (!"".equals(staff) && staff.contains("-")) {
                if (staff.endsWith("人")) {
                    // 最小员工
                    minStaff = Integer.parseInt(staff.substring(0, staff.indexOf("-")));
                    // 最大员工
                    maxStaff = Integer.parseInt(staff.substring(staff.indexOf("-") + 1, staff.indexOf("人")));
                }

            } else if (staff.endsWith("上")) {
                // 最小员工
                minStaff = 0;
                // 最大员工
                maxStaff = Integer.parseInt(staff.substring(0, staff.indexOf("人")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        Integer[] staffs = {minStaff, maxStaff};
        return staffs;
    }

    /**
     * 处理工资数据
     *
     * @param wage 工资
     * @return float[]
     */
    private float[] processSalaryBoss(String wage) {
        float minWage = 0;
        float maxWage = 0;

        try {
            if (!"".equals(wage) && wage.contains("-")) {
                if (wage.endsWith("K")) {
                    // K的最小工资
                    minWage = Float.parseFloat(wage.substring(0, wage.indexOf("-"))) * 1000;
                    // K的最大工资
                    maxWage = Float.parseFloat(wage.substring(wage.indexOf("-") + 1, wage.indexOf("K"))) * 1000;
                } else if (wage.endsWith("天")) {
                    // 天的最小工资
                    minWage = Float.parseFloat(wage.substring(0, wage.indexOf("-"))) * 30;
                    // 天的最大工资
                    maxWage = Float.parseFloat(wage.substring(wage.indexOf("-") + 1, wage.indexOf("元"))) * 30;
                } else if (wage.endsWith("薪")) {
                    // 获取月份
                    int month = Integer.parseInt(wage.substring(wage.indexOf("·") + 1, wage.indexOf("薪")));
                    // K的最小工资
                    minWage = ((Float.parseFloat(wage.substring(0, wage.indexOf("-"))) * 1000) * month) / 12.0f;
                    // K的最大工资
                    maxWage = ((Float.parseFloat(wage.substring(wage.indexOf("-") + 1, wage.indexOf("K"))) * 1000) * month) / 12.0f;
                }
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }



        float[] wages = {minWage, maxWage};
        return wages;
    }



    /**
     * 处理工资数据
     *
     * @param wage 工资
     * @return float[]
     */
    private float[] processSalary51(String wage) {
        float minWage = 0;
        float maxWage = 0;




        try {
            if (!"".equals(wage)&&wage.contains("-")){
                if (wage.endsWith("月")){
                    if (wage.contains("万")){
                        minWage =Float.parseFloat(wage.substring(0,wage.indexOf("-")))*10000;
                        maxWage =Float.parseFloat(wage.substring(wage.indexOf("-")+1,wage.indexOf("万")))*10000;
                    }else{
                        minWage =Float.parseFloat(wage.substring(0,wage.indexOf("-")))*1000;
                        maxWage =Float.parseFloat(wage.substring(wage.indexOf("-")+1,wage.indexOf("千")))*1000;
                    }
                }else{
                    if (wage.contains("万")){
                        minWage =Float.parseFloat(wage.substring(0,wage.indexOf("-")))*10000/12;
                        maxWage =Float.parseFloat(wage.substring(wage.indexOf("-")+1,wage.indexOf("万")))*10000/12;
                    }else{
                        minWage =Float.parseFloat(wage.substring(0,wage.indexOf("-")))*1000/12;
                        maxWage =Float.parseFloat(wage.substring(wage.indexOf("-")+1,wage.indexOf("千")))*1000/12;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        float[] wages = {minWage, maxWage};
        return wages;
    }


}
